﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Xml.Linq;

namespace WebEngine
{
	/// <summary>
	/// Summary description for Tips
	/// </summary>
	[WebService(Namespace = "http://tempuri.org/")]
	[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
	[System.ComponentModel.ToolboxItem(false)]
	// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
	// [System.Web.Script.Services.ScriptService]
	public class Tips : System.Web.Services.WebService
	{

		[WebMethod]
		public string GetNextRandomTip()
		{

			// Loading from a file
			XDocument loaded = XDocument.Load(Server.MapPath(@"App_Data\Tips.xml"));

			int tipsCount = loaded.Elements("Tips").Elements("Tip").Count();

			Random rnd = new Random();
			int randomTip = rnd.Next(1, tipsCount);

			// Query the random tip
			var tipsQuery = from tip in loaded.Elements("Tips").Elements("Tip")
						where (int)tip.Attribute("id") == randomTip
						select tip.Value;


			// Returning the random tip
			return tipsQuery.First();
		}
	}
}
